package com.yjhb.android.sbz.fragment;

import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.TaskListItemAdapter;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.Label;
import com.yjhb.android.sbz.entity.TaskInfo;
import com.yjhb.android.sbz.mvp.hall.HallTaskListPresenter;
import com.yjhb.android.sbz.mvp.hall.HallTaskListView;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.popup.TypePopup;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.tool.ZxTool;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.jojo.android.zxlib.view.dialog.ILoading;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/11/18 16:24
 */
public class TaskListFragment extends BaseFragment<HallTaskListPresenter> implements OnRefreshLoadmoreListener, HallTaskListView, QMUITabSegment.OnTabSelectedListener, PopupWindow.OnDismissListener, TypePopup.onSelectedLabelListener, BaseRecyclerAdapter.ItemClickListener<TaskInfo> {
    private final String TAG = "TaskListFragment";

    @BindView(R.id.recyclerView)
    EmptyRecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.layout_empty)
    View empty_layout;
    @BindView(R.id.sort_tabLayout)
    QMUITabSegment sort_tabLayout;
    @BindView(R.id.type_tv)
    TextView type_tv;
    @BindView(R.id.filterView)
    View filterView;
    @BindView(R.id.title_layout)
    View title_layout;
    ILoading loadingDialog;


    TaskListItemAdapter adapter;

    List<TaskInfo> tasks = new ArrayList<>();

    List<Label> labels = new ArrayList<>();
    TypePopup typePopup = null;


    private final int size = 25;
    private int current = 1;
    private int sort = 1; //排序方式
    private Integer categoryId = null; //分类id
    private Integer type = null; //类型  全部、简单、高价

    @Override
    protected HallTaskListPresenter initPresenter() {
        return new HallTaskListPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_hall_list;
    }

    @Override
    protected void initUI() {
        sort_tabLayout.setDefaultSelectedColor(getResources().getColor(R.color.red_31));
        sort_tabLayout.setDefaultNormalColor(getResources().getColor(R.color.gray_66));
        sort_tabLayout.setTabTextSize(ZxTool.dip2px(14));
        sort_tabLayout.setIndicatorWidthAdjustContent(false);
        sort_tabLayout.addTab(new QMUITabSegment.Tab("综合"));
        sort_tabLayout.addTab(new QMUITabSegment.Tab("人气"));
        sort_tabLayout.addTab(new QMUITabSegment.Tab("最新"));
        sort_tabLayout.addTab(new QMUITabSegment.Tab("价格"));
        sort_tabLayout.selectTab(0);
        recyclerView.setEmptyView(empty_layout);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter = new TaskListItemAdapter(getContext()));
        adapter.setData(tasks);
        initLabel(type);
        typePopup = new TypePopup(getContext(), labels);
        loadingDialog = LoadingDialogTool.createDialog(this);
    }

    @Override
    protected void initEvent() {
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
        sort_tabLayout.addOnTabSelectedListener(this);
        typePopup.setOnDismissListener(this);
        typePopup.setOnSelectedLabelListener(this);
        adapter.setOnItemClick(this);
    }

    @Override
    protected void initData() {
        request(true, true);
    }

    public void request(boolean showLoading) {
        request(showLoading, false);
    }

    public void request(boolean showLoading, boolean refresh) {
        if (showLoading)
            if (loadingDialog != null)
                loadingDialog.show();
        if (refresh) {
            current = 1;
            categoryId = null;
            if (typePopup != null)
                typePopup.selectedFirst();
        }
        if (presenter != null)
            presenter.loadHallList(sort, type, categoryId, size, current);
    }

    @Override
    public void onUserVisibleChanged(boolean visible) {
        super.onUserVisibleChanged(visible);
        if (visible) {
            request(true, true);
        }
    }

    /**
     * 初始化标签
     */
    private void initLabel(Integer type) {
        String s = null;

        labels.clear();
        Label label = new Label();
        label.setTitle("全部");
        label.setTag(null);
        labels.add(label);

        if (type == null || type == 2) {
            label = new Label();
            label.setTitle(s = getString(R.string.task_type_download_app));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);


            label = new Label();
            label.setTitle(s = getString(R.string.task_type_register));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);

            label = new Label();
            label.setTitle(s = getString(R.string.task_type_approve));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);

            label = new Label();
            label.setTitle(s = getString(R.string.task_type_eCommerce));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);

            label = new Label();
            label.setTitle(s = getString(R.string.task_type_securities));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);
        }

        if (type == null || type == 1) {
            label = new Label();
            label.setTitle(s = getString(R.string.task_type_follow));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);


            label = new Label();
            label.setTitle(s = getString(R.string.task_type_vote));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);

            label = new Label();
            label.setTitle(s = getString(R.string.task_type_bargain));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);


            label = new Label();
            label.setTitle(s = getString(R.string.task_type_forward));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);

            label = new Label();
            label.setTitle(s = getString(R.string.task_type_help));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);
        }
        if (type == null) {

            label = new Label();
            label.setTitle(s = getString(R.string.task_type_other));
            label.setTag(Tool.publishTypeTransformID(s));
            labels.add(label);

        }

    }

    @OnClick(R.id.type_tv)
    void type_tv(TextView view) {
        view.setSelected(!view.isSelected());
        if (view.isSelected()) {
            showTypePopup();
            view.setTextColor(ResManager.getThemeColor());
            view.getCompoundDrawables()[2].setTint(ResManager.getThemeColor());
            EventBus.getDefault().post(new EventMsg(EventMsg.DISPLAY_BOTTOM_MASK, null));
            filterView.setVisibility(View.VISIBLE);
        } else {
            view.setTextColor(getResources().getColor(R.color.gray_66));
            view.getCompoundDrawables()[2].setTint(getResources().getColor(R.color.gray_66));
            EventBus.getDefault().post(new EventMsg(EventMsg.HIDE_BOTTOM_MASK, null));
            filterView.setVisibility(View.GONE);
        }
    }


    private void showTypePopup() {
        typePopup.showAsDropDown(title_layout);
    }

    @Override
    public void onSelectedLabel(Label label) {

    }

    @Override
    public void onClickSelectedLabel(Label label) {
        current = 1;
        categoryId = (Integer) label.getTag();
        request(true);
    }

    @Override
    public void onDismiss() {
        type_tv(type_tv);
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        current++;
        request(false);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        request(false);
    }

    @Override
    public void onListData(List<TaskInfo> tasks, boolean isRefresh) {
        loadingDialog.cancel();
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadmore();
            smartRefreshLayout.finishRefresh();
        }
        if (isRefresh) {
            this.tasks.clear();
        } else {
            if (type == null && (tasks == null || tasks.size() == 0) && this.tasks != null) {
                current = 1;
                List<TaskInfo> taskInfos = new ArrayList<>();
                for (TaskInfo taskInfo : this.tasks) {
                    taskInfos.add(taskInfo);
                    if (taskInfos.size() >= 15) {
                        break;
                    }
                }
                tasks = taskInfos;
            }
        }
        this.tasks.addAll(tasks);
        adapter.notifyDataSetChanged();
        if (isRefresh) {
            recyclerView.scrollToPosition(0);
        }
    }

    @Override
    public void onFailure(int code, String msg) {
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadmore();
        }
        if (loadingDialog != null)
            loadingDialog.cancel();
        ZxToast.showINFOBottomToast(msg);
    }

    @Override
    public void onTabSelected(int index) {
        sort = index + 1;
        request(true);
    }

    @Override
    public void onTabUnselected(int index) {

    }

    @Override
    public void onTabReselected(int index) {

    }

    @Override
    public void onDoubleTap(int index) {

    }


    @Override
    public void onItemClick(int index, TaskInfo task) {
        Tool.startEditTaskDetailActivity(getContext(), task.getId());
    }

    @Override
    public void onViewClick(View view, String msg, int index, TaskInfo task) {

    }

    public void setType(Integer type) {
        this.type = type;
    }
}
